package com.smzd.ccms.core;


public class ViewMapResult implements Comparable<ViewMapResult> {
	private boolean match;
	private String message;
	private String[] variables;
	private String[] values;
	private ViewMapper mapper;

	public boolean isMatch() {
		return match;
	}

	public void setMatch(boolean match) {
		this.match = match;
	}

	public String[] getVariables() {
		return variables;
	}

	public void setVariables(String[] variables) {
		this.variables = variables;
	}

	public String[] getValues() {
		return values;
	}

	public void setValues(String[] values) {
		this.values = values;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public ViewMapper getMapper() {
		return mapper;
	}

	public void setMapper(ViewMapper mapper) {
		this.mapper = mapper;
	}

	/**
	 * 匹配原则：
	 * 1. 固定部分匹配长度越大越好
	 * 2. 固定部分匹配长度相等的情况下，正则表达式越多越好
	 */
	@Override
	public int compareTo(ViewMapResult that) {
		int thisFixCount = this.mapper.getFixCount();
		int thatFixCount = that.mapper.getFixCount();
		if(thisFixCount!=thatFixCount){
			return thatFixCount-thisFixCount;
		}
		int thisCount = this.mapper.getVariableCount();
		int thatCount = that.mapper.getVariableCount();
		return thisCount-thatCount;
	}
	
	public static void main(String[] args){
		ViewMapper m0 = new ViewMapper("/p/site/channel/page-{1}.html", "No0");
		ViewMapper m1 = new ViewMapper("/p/site/channel/page-{1}-def.html", "No1");
		ViewMapper m2 = new ViewMapper("/p/site/channel/page-{1}-{abc}-def.html", "No2");
		ViewMapper m3 = new ViewMapper("/p/site/channel/page-{1}-abc-def.html", "No3");
//		ViewMapResult r1 = new ViewMapResult();
//		r1.setMapper(m1);
//		ViewMapResult r2 = new ViewMapResult();
//		r2.setMapper(m2);
//		ViewMapResult r3 = new ViewMapResult();
//		r3.setMapper(m3);
//		Set<ViewMapResult> set = new TreeSet<>();
//		set.add(r1);
//		set.add(r3);
//		set.add(r2);
//		ViewMapResult r = set.iterator().next();
//		System.out.println(r.getMapper().getPage());
		ViewMapperCache cache = ViewMapperCache.getInstance();
		cache.addViewMapper(m1).addViewMapper(m2).addViewMapper(m3).addViewMapper(m0);
		ViewMapResult result = ViewMapperCache.getViewMapperResult("/p/site/channel/page-xxxx-abcdef-def.html");
		String[] variables = result.getVariables();
		String[] values = result.getValues();
		
		System.out.println(result.getMapper().getPage());
		for(int i=0; i<values.length; i++){
			System.out.println(variables[i]+"="+values[i]);
		}
		
	}
	
	@Override
	public String toString(){
		StringBuffer sb = new StringBuffer("match result: ").append(this.isMatch()).append(", pattern: ").append(this.mapper.getPattern()).append(", message: ").append(this.getMessage());
		
		return sb.toString();
	}
}
